package com.qc.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qc.dto.CookDetail;
import com.qc.dto.Result;
import com.qc.enity.Dish;
import com.qc.exception.ResultCodeEnum;
import com.qc.service.IDishService;
import com.qc.utils.JavaConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜谱相关操作控制层
 */
@RestController
@RequestMapping("dish")
@CrossOrigin
public class DishController {

    @Autowired
    private IDishService dishService;

    /** 获取菜谱列表 */
    @GetMapping("list/{current}/{size}")
    public Result<IPage<Dish>> dishList (@PathVariable int current, @PathVariable int size){

        Page page = new Page(current, size);
        IPage<Dish> data = dishService.dishList(page);
        return Result.successResult(data);
    }
    /** 删除菜谱 */
    @GetMapping("delete/{dishId}")
    public Result<IPage<Dish>> deleteDishList (@PathVariable Integer dishId
    ){
        System.out.println(dishId);
        Integer rows = dishService.deleteDishList(dishId) ;
        if (rows != 1){
            return Result.error(ResultCodeEnum.GOODS_ADD_FAIL.getCode(), ResultCodeEnum.GOODS_ADD_FAIL.getMsg());
        }
        return Result.successResultOnlyMessage(ResultCodeEnum.SUCCESS.getMsg());
    }
    /** 获取降序菜谱列表 descDishList */
    @GetMapping("list/descDishList/{current}/{size}")
    public Result<List<Dish>> descDishList (@PathVariable int current, @PathVariable int size){

        Page page = new Page(current, size);
        List<Dish> data = dishService.descDishList(page);
        return Result.successResult(data);
    }
    /** 添加菜谱 */
    @PostMapping("add")
    public Result<IPage<Dish>> addDishList (@RequestBody Dish dish
                                            ){
        System.out.println(dish);
        Integer rows = dishService.addDishList(dish) ;
        if (rows != 1){
            return Result.error(ResultCodeEnum.GOODS_ADD_FAIL.getCode(), ResultCodeEnum.GOODS_ADD_FAIL.getMsg());
        }
        return Result.successResultOnlyMessage(ResultCodeEnum.SUCCESS.getMsg());
    }
    /** 搜索菜谱 */
    @GetMapping("select/{current}/{size}/{name}")
    public Result<IPage<Dish>> dishSelect(@PathVariable int current, @PathVariable int size, @PathVariable String name){

        Page page = new Page(current, size);
        IPage<Dish> data = dishService.dishSelect(page, name);
        //模糊失败
        if (data.getTotal() == 0){
            //这里说明模糊不到
            return Result.error(ResultCodeEnum.LIKE_NULL.getCode(), ResultCodeEnum.LIKE_NULL.getMsg());
        }

        return Result.successResult(data);

    }

    /** 菜品做法 */
    @GetMapping("cook/{dishId}")
    public Result<CookDetail> dishCook(@PathVariable Integer dishId){

        CookDetail data = dishService.dishCook(dishId);

        return Result.successResult(data);
    }
    /** 本周佳作（每周随机推荐一个菜品） */
    @GetMapping("commend/{dishId}")
    public Result<Dish> dishCommend(@PathVariable Integer dishId){
        //不为数据库中该有的dishId
        if (dishId > JavaConstant.DISH_COUNT || dishId < 0){
            return Result.error(ResultCodeEnum.DISH_ID_NOT_EXIST.getCode(), ResultCodeEnum.DISH_ID_NOT_EXIST.getMsg());
        }
        Dish data = dishService.dishCommend(dishId);
        return Result.successResult(data);

    }

    /** desc  */
    @GetMapping("commendAll")
    public Result<List> commendAll(){
        List data = dishService.commendAll();
        return Result.successResult(data);
    }

    /** 根据typeId查菜*/
    @GetMapping("types/{typeId}")
    public Result<List<Dish>> getDishByTypeId(@PathVariable Integer typeId) {

        //type不为空查询Dish列表
        if (typeId == null || typeId < 0) {
            return Result.error(ResultCodeEnum.TYPE_NULL.getCode(), ResultCodeEnum.TYPE_NULL.getMsg());
        }
        List<Dish> data=dishService.getDishByTypeId(typeId);

        if (data.isEmpty()){
            //这里说明模糊不到
            return Result.error(ResultCodeEnum.TYPE_DISH_NULL.getCode(), ResultCodeEnum.TYPE_DISH_NULL.getMsg());
        }
        return Result.successResult(data);
    }

    /** 根据typeId add Dian Zan */
    @GetMapping("addDishDianZan/{dishId}")
    public Result<Integer> addDishDianZan(@PathVariable Integer dishId) {
        Integer rows = dishService.addDishDianZan(dishId);

        if (rows != 1){
            //添加失败
            return Result.error(ResultCodeEnum.GOODS_ADD_FAIL.getCode(), ResultCodeEnum.GOODS_ADD_FAIL.getMsg());
        }
        return Result.successResultOnlyMessage(ResultCodeEnum.SUCCESS.getMsg());
    }

    /** 根据dishid 查询*/
    @GetMapping("dishIdChaXun/{dishId}")
    public Result<Dish> dishIdChaXun(@PathVariable Integer dishId) {
        Dish dish = dishService.dishIdChaXun(dishId);
        if (dish ==null){
            return Result.error(ResultCodeEnum.GOODS_ADD_FAIL.getCode(), ResultCodeEnum.GOODS_ADD_FAIL.getMsg());
        }
        return Result.successResult(dish);
    }

    /**
     * update dish by id
     */
    @PostMapping("updateDishById")
    public Result<Integer> updateDishById(@RequestBody Dish dish) {
        Integer s = dishService.updateDishById(dish);
        if (s == 0){
            return Result.error(ResultCodeEnum.GOODS_ADD_FAIL.getCode(), ResultCodeEnum.GOODS_ADD_FAIL.getMsg());
        }
        return Result.successResult(s);
    }
}
